package com.cn.chaomeng.vo.form;

import com.cn.chaomeng.dao.AdminMapper;
import com.cn.chaomeng.model.Admin;
import com.cn.chaomeng.model.example.AdminExample;
import com.cn.chaomeng.service.IAdminService;
import com.cn.chaomeng.vo.AjaxError;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LYJ
 *         Created by Administrator on 2018/3/5 0005.
 */
public class AdminUserForm {

    public static Admin toAdminUser(AdminUserForm adminUserForm){
        Admin admin = new Admin();
        admin.setId(adminUserForm.getId());
        admin.setUsername(adminUserForm.getUsername());
        admin.setPwd(adminUserForm.getPwd());
        return admin;
    }

    private IAdminService adminService;
    private Integer id;
    private String username;
    private String pwd;

    public AdminUserForm(IAdminService adminService){
        this.adminService = adminService;
    }

    public AjaxError login(){
        AjaxError error = new AjaxError();
        if(emptyUsername()){
            error.setError(1,"账号错误");
        }else{
            error = validatePwd();
            if(error.getRecode() != 0){
                return error;
            }else{
                error.setRecode(0);
            }
        }
        return error;
    }

    public boolean emptyUsername(){
        return adminService.selectByUsername(username) == null;
    }

    public AjaxError validatePwd(){
        AjaxError error = new AjaxError();
        Admin admin = adminService.selectByUsername(username);
        if(admin != null){
            if(admin.getPwd().equals(pwd)){
                error.setRecode(0);
            }else{
                error.setError(2,"密码错误");
            }
        }else{
            error.setError(1,"账号错误");
        }
        return error;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
